﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarNCDCapacityDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarNCDCapacityDB
    {
        public CarNCDCapacityDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarNCDCapacity carncdcapacity)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarNCDCapacityInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCarNCDCapId", carncdcapacity.ProviderCarNCDCapId);
            _sPara.Add("@ProviderCompanyId", carncdcapacity.ProviderCompanyId);
            _sPara.Add("@NCD", carncdcapacity.NCDPercent);
            _sPara.Add("@Capacity", carncdcapacity.CarCapacity);
            _sPara.Add("@Rate", carncdcapacity.Rate);
            _sPara.Add("@RefCEUserId", carncdcapacity.RefCEUserId);
            _sPara.Add("@Status", carncdcapacity.Status == true ? 1 : 0);
            _sPara.Add("@Finfo1", carncdcapacity.FInfo1);
            _sPara.Add("@Dinfo1", carncdcapacity.DInfo1);

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public DataTable GetCarNCDCapacityList(Int64 ProviderCompanyId, Int64 ProviderCarNCDCapId)
        {
            DataTable _carncdcapacitylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarNCDCapacitySelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@ProviderCarNCDCapId", ProviderCarNCDCapId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carncdcapacitylist = _dt;
                }
            }
            return _carncdcapacitylist;
        }

        public DataTable GetCarNCDCapacityList(Int64 ProviderCompanyId)
        {
            DataTable _carncdcapacitylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarNCDCapacitySelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carncdcapacitylist = _dt;
                }
            }
            return _carncdcapacitylist;
        }

        public CarNCDCapacity GetCarNCDCapacity(Int64 ProviderCarNCDCapId)
        {
            CarNCDCapacity _carncdcapacity = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarNCDCapacitySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarNCDCapId", ProviderCarNCDCapId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carncdcapacity = new CarNCDCapacity();
                    _carncdcapacity = FillDataRecord(_dt, 0);
                }
            }
            return _carncdcapacity;
        }

        public CarNCDCapacity GetCarNCDCapacity(Int64 ProviderCompanyId, string NCD, string Capacity)
        {
            CarNCDCapacity _carncdcapacity = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarNCDCapacityItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@NCD", NCD);
            _spara.Add("@Capacity", Capacity);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carncdcapacity = new CarNCDCapacity();
                    _carncdcapacity = FillDataRecord(_dt, 0);
                }
            }
            return _carncdcapacity;
        }

        private CarNCDCapacity FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarNCDCapacity myCarNCDCapacity = new CarNCDCapacity();
            myCarNCDCapacity.ProviderCarNCDCapId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarNCDCapId"].ToString());
            myCarNCDCapacity.ProviderCompanyId = Convert.ToInt32(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarNCDCapacity.NCDPercent = myDataTable.Rows[RowId]["NCD"].ToString();
            myCarNCDCapacity.CarCapacity = myDataTable.Rows[RowId]["Capacity"].ToString();
            myCarNCDCapacity.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarNCDCapacity.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarNCDCapacity.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarNCDCapacity.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarNCDCapacity.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarNCDCapacity.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarNCDCapacity;
        }

    }
}